/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gskorupa.univermon.storage;

import com.gskorupa.univermon.auth.Client;
import com.gskorupa.univermon.versatile.ReportEntity;
import com.gskorupa.univermon.versatile.Report;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

/**
 *
 * @author grzesk
 */
public class Persistent {
    
    private static volatile Persistent instance = new Persistent();
    
    public static Persistent getInstance() {
        return instance;
    }
    
    private Persistent() {
    }
    
    public Report getReport(Report report) {

        // get from start or from end of list depending on parameters
        // order by is not neede ???
        String maxResults = report.getLimit();
        report.clear();
        //report.setResults(max);
        PersistenceManager pm = PMF.get().getPersistenceManager();
//String filter="userUID == '"+report.getUser()+"";
        String filter1 = "SELECT FROM com.gskorupa.univermon.versatile.ReportEntity WHERE userUID == '" + report.getUser() + "'";
        String filter3 = " ORDER BY timestamp";
        String filter2 = "";
        if (!report.isFifo()) {
            filter3 = " ORDER BY timestamp DESC";
        }
        if (!report.getQuery().isEmpty()) {
            filter2 = " && " + report.getQuery();
        }
        String filter = filter1 + filter2 + filter3;
        System.out.println(filter);
        long limit = 0;
        try {
            limit = Long.parseLong(report.getLimit());
        } catch (NumberFormatException e) {
            
        } catch (NullPointerException e) {
            
        }

//Query q = pm.newQuery(ReportEntity.class,filter);
        Query q = pm.newQuery(filter);
        if (limit > 0) {
            q.setRange(0, limit);
        }
        List<ReportEntity> l = (List<ReportEntity>) q.execute();
        ReportEntity e;
        for (int i = 0; i < l.size(); i++) {
            e = (ReportEntity) l.get(i);
            report.append(pm.detachCopy(e));
            System.out.println(e.toString());
        }
        pm.close();
        return report;
    }
    
    public void addEntry(ReportEntity entity) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(entity);
            System.out.println("Storing " + entity.toString());
        } finally {
            pm.close();
        }
    }
    
    public void addUser(Client client) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(client);
            System.out.println("Storing " + client.toString());
        } finally {
            pm.close();
        }
    }
    
    public Client getClient(String uid) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        String filter = "SELECT FROM com.gskorupa.univermon.auth.Client WHERE uid == '" + uid + "'";
        Query q = pm.newQuery(filter);
        List<Client> l = (List<Client>) q.execute();
        if(l.size()>0){
            Client detached=pm.detachCopy(l.get(0));
            pm.close();
            return detached;
        }
        return null;
    }
    
    public boolean confirmRegistration(String uid){
        PersistenceManager pm = PMF.get().getPersistenceManager();
        Transaction trans = pm.currentTransaction();
        boolean result=false;
        trans.begin();
        Query q = pm.newQuery("SELECT FROM com.gskorupa.univermon.auth.Client WHERE uid == '" + uid + "'");
        List<Client> l = (List<Client>) q.execute();
        if(l.size()>0){
            Client client = l.get(0);
            client.setConfirmed(true);
            result=true;
        }
        trans.commit();
        return result;
    }
}
